//
//  PhotoShowCollectionView.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/2.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "PhotoShowCollectionView.h"
#import "PhotoModel.h"
#import "PhotoShowViewController.h"
#import "PhotoSetCollectionViewCell.h"

@implementation PhotoShowCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        
        self.delegate = self;
        self.dataSource = self;
        self.pagingEnabled = YES;
        
        self.showsHorizontalScrollIndicator = NO;
        
        self.backgroundColor = [UIColor clearColor];
        
        [self registerClass:[PhotoSetCollectionViewCell class] forCellWithReuseIdentifier:kPhotoSetCollectionViewCellId];
    }
    return self;
}

- (void)setPhotoSetModel:(PhotoSetModel *)photoSetModel {
    if (_photoSetModel != photoSetModel) {
        _photoSetModel = photoSetModel;
        
        [self reloadData];
    }
}

#pragma mark - collectionView协议方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_photoSetModel.imgCount integerValue];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoSetCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPhotoSetCollectionViewCellId forIndexPath:indexPath];
    
    cell.model = _photoSetModel.photoModelArray[indexPath.row];

    return cell;
}



//cell出现时，刷新相应UI
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //通过响应者链,找到PhotoShowViewController
    PhotoShowViewController* photoVC = [Tools className:@"PhotoShowViewController" fromResponder:self.nextResponder];
    
    if (photoVC) {
        UILabel* contentLabel = (UILabel*)[photoVC.view viewWithTag:1002];
        UILabel* currentIndexLabel = (UILabel*)[photoVC.view viewWithTag:1001];
        
        //每次cell出现的时候，在这里重新设置photoVC的这个值。于是当保存图片按钮被点击时，那边就可以知道保存哪一张图片了
        photoVC.currentShowPhotoIndex = indexPath.row;
        
        PhotoModel* photoModel = _photoSetModel.photoModelArray[indexPath.row];
        //当前图片序号
        currentIndexLabel.text = [NSString stringWithFormat:@"%li", indexPath.row+1];
        
        //新闻内容
        contentLabel.text = photoModel.content;
    }
}


@end
